class Solution {
public:
    int row[8] = { -1,-1,-1,0,0,1,1,1 };
    int col[8] = { -1,0,1,-1,1,-1,0,1 };
    int m, n;
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        m = board.size(), n = board[0].size();
        int x = click[0], y = click[1];
        if (board[x][y] == 'M')
        {
            board[x][y] = 'X';
            return board;
        }
        else if (board[x][y] == 'E')
        {
            int sum = inspect(board, x, y);
            if (sum)
            {
                board[x][y] = sum + '0';
                return board;

            }
            else {
                board[x][y] = 'B';
                dfs(board, x, y);
                return board;
            }
        }
        return board;

    }
    void dfs(vector<vector<char>>& board, int r, int c)
    {
        for (int i = 0;i < 8;i++)
        {
            int x = r + row[i], y = c + col[i];
            if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E')
            {
                int tmp = inspect(board, x, y);
                if (tmp)
                {
                    board[x][y] = '0' + tmp;
                }
                else
                {
                    board[x][y] = 'B';
                    dfs(board, x, y);
                }

            }
        }

    }
    int inspect(vector<vector<char>>& board, int r, int c)
    {
        int sum = 0;
        for (int i = 0;i < 8;i++)
        {
            int x = r + row[i], y = c + col[i];
            if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M') sum++;
        }
        return sum;
    }
};